package com.huobao.yunying.activitys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.zhangying.R;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;

import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

/**
 * Created by Administrator on 2016/8/18.
 */
public class Act_Select_String<T> extends HuobaoBaseActivity implements AdapterView.OnItemClickListener{

	private SelectAdapter adapter;
	@ViewInject(R.id.lv_select)
	private ListView listview;

	@Override
	protected int getContentView() {
		return R.layout.layout_select_string;
	}

	@Override
	protected String getTitleText() {
		return getIntent().getStringExtra("title");
	}

	@Override
	protected void init() {
		List<T> list = (List<T>) getIntent().getSerializableExtra("data");
		String field = getIntent().getStringExtra("field");
		adapter = new SelectAdapter(context, list,field);
		listview.setOnItemClickListener(this);
		listview.setAdapter(adapter);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
		T t = adapter.getItem(i);
		Intent intent = new Intent();
		intent.putExtra("data", (Serializable) t);
		setResult(RESULT_OK, intent);
		finish();
	}

	private class SelectAdapter extends QuickAdapter<T> {
		private String fieldName;
		public SelectAdapter(Context context, List<T> data, String field) {
			super(context, android.R.layout.simple_list_item_1, data);
			this.fieldName = field;
		}

		@Override
		protected void convert(BaseAdapterHelper helper, T t) {
			Class cls = t.getClass();
			try {
				Field field = cls.getField(fieldName);
				field.setAccessible(true);
				Object val = field.get(t);
				helper.setText(android.R.id.text1, val.toString());
			} catch (Exception e) {
			}
		}
	}
}
